package cn.itsource.pethome.org.service.impl;

import cn.itsource.pethome.basic.service.impl.BaseServiceImpl;
import cn.itsource.pethome.basic.util.MD5Utils;
import cn.itsource.pethome.basic.util.StrUtils;
import cn.itsource.pethome.org.domain.Employee;
import cn.itsource.pethome.org.mapper.EmployeeMapper;
import cn.itsource.pethome.org.service.IEmployeeServive;
import cn.itsource.pethome.user.domain.LoginInfo;
import cn.itsource.pethome.user.mapper.LoginInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EmployeeServiveImpl extends BaseServiceImpl<Employee> implements  IEmployeeServive {

    @Autowired
    EmployeeMapper employeeMapper;
    @Autowired
    private LoginInfoMapper loginInfoMapper;

    @Override
    public void save(Employee employee) {
        //给employee设置盐值
        employee.setSalt(StrUtils.getComplexRandomString(4));
        //给employee设置密码
        employee.setPassword(MD5Utils.encrypByMd5(employee.getPassword()+employee.getSalt()));
        //员工对象转为登录信息
        LoginInfo loginInfo = employee2LoginInfo(employee);
        //保存登录信息
        loginInfoMapper.save(loginInfo);
        //设置登录信息
        employee.setLoginInfo(loginInfo);
        //保存员工信息

        super.save(employee);
    }


    private LoginInfo employee2LoginInfo(Employee employee) {
        LoginInfo loginInfo = new LoginInfo();
        BeanUtils.copyProperties(employee,loginInfo);
        //后端管理人员
        loginInfo.setSalt(employee.getSalt());
        loginInfo.setType(false);
        return loginInfo;
    }
}
